package org.example.spring_blog.interceptor;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.example.spring_blog.util.jwtUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String jwt=request.getHeader("user_token_header");
        log.info("从token中获取信息，token：",jwt);

        Claims claims = jwtUtils.parseJwt(jwt);
        if(claims==null) {
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
